var scheduleArr;
var calUtil = {
    getDaysInmonth : function(iMonth, iYear) {
        // alert(iMonth)
        var dPrevDate = new Date(iYear, iMonth, 0);
        return dPrevDate.getDate();
    },
    bulidCal : function(iYear, iMonth) {
        var aMonth = new Array();
        aMonth[0] = new Array(7);
        aMonth[1] = new Array(7);
        aMonth[2] = new Array(7);
        aMonth[3] = new Array(7);
        aMonth[4] = new Array(7);
        aMonth[5] = new Array(7);
        aMonth[6] = new Array(7);
        var dCalDate = new Date(iYear, iMonth - 1, 1);
        var iDayOfFirst = dCalDate.getDay();
        var iDaysInMonth = calUtil.getDaysInmonth(iMonth, iYear);
        var iVarDate = 1;
        var d,w;
        aMonth[0][0] = "日";
        aMonth[0][1] = "一";
        aMonth[0][2] = "二";
        aMonth[0][3] = "三";
        aMonth[0][4] = "四";
        aMonth[0][5] = "五";
        aMonth[0][6] = "六";
        for ( d = iDayOfFirst; d < 7; d++) {
            aMonth[1][d] = iVarDate;
            iVarDate++;
        }
        for ( w = 2; w < 7; w++) {
            for ( d = 0; d < 7; d++) {
                if (iVarDate <= iDaysInMonth) {
                    aMonth[w][d] = iVarDate;
                    iVarDate++;
                }
            }
        }
        return aMonth;
    },
    ifHasSigned : function(signList, day) {
        var signed = false;
        $.each(signList, function(index, item) {
            var myDay = Number(item.vacationDateStr.split('-')[2]);
            if (myDay == day && item.fullFlag) {
                signed = true;
                return false;
            }
        });
        return signed;
    },
    drawCal : function(iYear, iMonth, signList, week) {
        // alert(signList)
        var scheduleDate= localStorage.getItem("scheduleDate");
        scheduleArr=scheduleDate.split('/');
        
        var myMonth = calUtil.bulidCal(iYear, iMonth);
        var htmls = new Array();
        
        htmls.push("<div class='sign_main' id='sign_layer'>");
        htmls.push("<div class='sign' id='sign_cal'>");
        htmls.push("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        htmls.push("<tr>");
        htmls.push("<td>" + myMonth[0][0] + "</td>");
        htmls.push("<td>" + myMonth[0][1] + "</td>");
        htmls.push("<td>" + myMonth[0][2] + "</td>");
        htmls.push("<td>" + myMonth[0][3] + "</td>");
        htmls.push("<td>" + myMonth[0][4] + "</td>");
        htmls.push("<td>" + myMonth[0][5] + "</td>");
        htmls.push("<td class='td_r'>" + myMonth[0][6] + "</td>");
        htmls.push("</tr>");
        
        var d,
            w;
        
        for ( w = 1; w < 7; w++) {

            //-1：全日历；数字：本周日历
            if(week == -1 || week == w) {

                if (w == 6 && myMonth[6][0] == undefined) {
                    break;
                }
                htmls.push("<tr week="+w+">");
                
                for ( d = 0; d < 7; d++) {
                    
                    var ifHasSigned = calUtil.ifHasSigned(signList, myMonth[w][d]);
                    var n = !isNaN(myMonth[w][d]) ? myMonth[w][d] : " ";
                    if(n<10){
                        var nStr = iYear +'-'+iMonth+"-0"+n;
                    }else{
                        var nStr = iYear +'-'+iMonth+"-"+n;
                    }

                    var classStr = 'item ';
                    if (d == 6) {
                        classStr += 'td_r ';
                        if (w == myMonth.length - 2 || (w == 6 && myMonth[6][0] == undefined && w == myMonth.length)) {
                            classStr += 'td_b';
                        }
                    } else if (w == myMonth.length - 2 || (w == 6 && myMonth[6][0] == undefined && w == myMonth.length)) {
                        classStr += 'td_b ';
                        if (d == 6) {
                            classStr += 'td_r';
                        }
                    }
                    
                    //htmls.push("<td class='"+classStr+"' name='"+nStr+"'> " + n + "</td>");
                    
                    // alert(n)
                    //考勤样式
                    if(isScheduled(n)) {
                        
                        var tempHtml = '';
                        tempHtml += '<div ontouchstart="" style="border-radius: 50% 0 0 50%; ">';
                        tempHtml += '<div class="" style="border-radius: 50%; background-color: #80C468; color: #FFFFFF;">';
                        tempHtml += ''+n+'';
                        tempHtml += '</div>';
                        tempHtml += '</div>';
                        htmls.push("<td class='"+classStr+"' name='"+nStr+"'> " + tempHtml + "</td>");
                        
                    }else {
                        htmls.push("<td class='"+classStr+"' name='"+nStr+"'> " + n + "</td>");
                    }
                
                }
                htmls.push("</tr><tr name='itemCon' class='uhide'><td colspan='7' class='it_c td_r'></td></tr>");
            }
        }
        htmls.push("</table>");
        htmls.push("</div>");
        htmls.push("</div>");
        return htmls.join('');
    }
}; 

function isScheduled (n) {
    var len=scheduleArr.length;
    for(var i=0;i<len;i++)
    {
        if(scheduleArr[i]==n)
        return true;
    }
  return false;
}